package com.blog.dao;
import com.blog.entity.message;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MessageDao  extends BaseDAO{

	private static MessageDao sd=null;

	public static MessageDao getInstance(){
		if (sd == null) {
			sd = new MessageDao();
		}
		return sd;
	}
	
	public List<message> getList() throws SQLException{
		List<message> messages = new ArrayList<>();
		String sql = "select * from  blog_message";
		rs = db.executeQuery(sql);
		while(rs.next()){
			message message= new message();
			message.setId(rs.getInt("id"));
			message.setTitle(rs.getString("title"));
			message.setImages(rs.getString("image"));
			message.setContent(rs.getString("content"));
			messages.add(message);
		}
		return messages;
	}
	
	public message getMessageById(int id) throws SQLException{
		message message= new message();
		String sql = "select * from  blog_message where id = ? ";
		Object[] params = {id};
		rs = db.executeQuery(sql, params);
		if(rs.next()){
			message.setId(rs.getInt("id"));
			message.setTitle(rs.getString("title"));
			message.setImages(rs.getString("image"));
			message.setContent(rs.getString("content"));
		}
		return message;
	}

}
